﻿using Serilog;
using ILogger = Serilog.ILogger;

namespace Nasty.Core.Log
{
    /// <summary>
    /// 日志
    /// </summary>
    public static class SerilogLogger
    {
        private static readonly ILogger logger;

        static SerilogLogger()
        {
            logger = new LoggerConfiguration()
                .WriteTo.File(
                    path: "logs/log-.txt",
                    rollingInterval: RollingInterval.Day, // 按天滚动
                    fileSizeLimitBytes: 10 * 1024 * 1014, // 10MB文件大小限制
                    rollOnFileSizeLimit: true, // 达到文件大小时滚动
                    shared: true, // 支持多进程共享
                    retainedFileCountLimit: 31, // 保留31天的日志文件
                    flushToDiskInterval: TimeSpan.FromSeconds(5))
                .CreateLogger();
        }

        public static void LogInfo(string message)
        {
            logger.Information(message);
        }

        public static void LogError(string message, Exception ex = null)
        {
            if (ex != null)
                logger.Error(ex, message);
            else
                logger.Error(message);
        }

        public static void LogWarning(string message)
        {
            logger.Warning(message);
        }

        public static void CloseAndFlush()
        {
            Serilog.Log.CloseAndFlush();
        }
    }
}
